package com.xiaoxueqi.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoxueqi.entity.Answer;
import com.xiaoxueqi.entity.Question;
import com.xiaoxueqi.mapper.AnswerMapper;
import com.xiaoxueqi.mapper.QuestionMapper;
import com.xiaoxueqi.service.QuestionProduceService;
import com.xiaoxueqi.utils.CertificateUtil.DateTimeUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
@RequiredArgsConstructor
@Transactional
public class QuestionProduceServiceImpl extends ServiceImpl<QuestionMapper,Question> implements QuestionProduceService {

    private final QuestionMapper questionMapper;
    private final AnswerMapper answerMapper;
    private String res;

    public  String search(String regex){
        String text=new String(res);
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(text);
        String res0=new String();
        if (matcher.find()) {
            res0 = matcher.group(1).trim();
            return res0;
        } else {
            return "没有找到";
        }
    }
    @Override
    public Question questionProduceService(String res,Question question){
        this.res=res;
        String regex00 = "答案(.*?)(?=。)";
        String regex0=search(regex00);
        String TrueOption=regex0;
        System.out.println(regex0);
        switch (question.getQuType()){
            case 1:
                switch (regex0){
                case "A":
                    TrueOption="0";
                    break;
                case "B":
                    TrueOption="1";
                    break;
                case "C":
                    TrueOption="2";
                    break;
                case "D":
                    TrueOption="3";
                    break;
                }
                break;
            case 2:
                switch (regex0){
                    case "A":
                        TrueOption="0";
                        break;
                    case "B":
                        TrueOption="1";
                        break;
                    case "C":
                        TrueOption="2";
                        break;
                    case "D":
                        TrueOption="3";
                        break;
                }
                break;
            case 3:
                switch (regex0){
                case "True":
                    TrueOption="0";
                    break;
                case "False":
                    TrueOption="1";
                    break;
            }
            break;
        }
        System.out.println(TrueOption);

        String regex1 = "题目：(.*?)(?=答案)";
        String questionContent=search(regex1);
        System.out.println(questionContent);

        String regex2 = "答案(.*?)(?=\",\"is_truncated\")";
        String questionAnalysyis=search(regex2);
        System.out.println(questionAnalysyis);

        question.setQuContent(questionContent);
        question.setAnalysis(questionAnalysyis);
        Date date=DateTimeUtil.getCurrentDate();
        question.setImage("");
        System.out.println("图片在这里"+question.getImage());
        question.setCreateTime(date);
//        question.setCreatePerson("tys");
        question.setImage("");
        questionMapper.insert(question);

        System.out.println("1111111111111111111111111111111");
        System.out.println("id"+question.getQuBankId());
        System.out.println("name"+question.getQuBankName());

        if(question.getQuType()==4){
            return question;
        }

        Answer answer = new Answer();
        answer.setImages("");
        answer.setQuestionId(question.getId());
        answer.setAnalysis(question.getAnalysis());
//        String questionRegex0="题目：(.*?)(?=\n)";
//        answer.setQuContent(search(questionRegex0));
        if(question.getQuType()==3){
            answer.setAllOption("对,"+"错");
            answer.setTrueOption(TrueOption);
            answerMapper.insert(answer);
            return question;
        }
        String questionRegex1="A(.*?)(?=B)";
        System.out.println(search(questionRegex1));
        String questionRegex2="B(.*?)(?=C)";
        String questionRegex3="C(.*?)(?=D)";
        String questionRegex4="D(.*?)(?=答案)";
        answer.setAllOption(search(questionRegex1)+","+search(questionRegex2)+","+search(questionRegex3)+","+search(questionRegex4));
        answer.setTrueOption(TrueOption);

        answerMapper.insert(answer);
        return question;
    }
}
